//
//  DestinationAlbumViewController.m
//  EDPS
//
//  Created by Neil on 15/11/26.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DestinationAlbumViewController.h"
#import "DestinationAlbumCollectionViewCell.h"
#import "ZWY_NetHandle.h"
#import "BaseModel.h"
#import "item.h"
#import "UIImageView+WebCache.h"
#import "AlbumDetailsViewController.h"

@interface DestinationAlbumViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *itemArr;

@end

@implementation DestinationAlbumViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationItem.title = @"相册";
    UIView *view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, self.view.frame.size.width, 1)];
    [self.view addSubview:view];
    [self getData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 获取数据
- (void)getData
{
    self.itemArr = [NSMutableArray array];
    NSString *url = [NSString stringWithFormat:@"%@/photos/?gallery_mode=1&count=18&sign=d6efc50b0a50345b147f1aada047e444", self.strUrl];
    [ZWY_NetHandle getURL:url Cookie:nil parameter:nil result:^(id result) {
        self.itemArr = [item transformWithArray:result[@"items"]];
        [self creatCollectionView];
    }];
}
#pragma mark - 创建collectionView

- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth / 2 - 20), 150);
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 10, 15);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,  ScreenWidth / 6 , ScreenWidth, ScreenHeight) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[DestinationAlbumCollectionViewCell class] forCellWithReuseIdentifier:@"twoCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
}
#pragma mark - collectionView方法
#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    AlbumDetailsViewController *DetailVc = [[AlbumDetailsViewController alloc]init];
    DetailVc.imgArr = self.itemArr;
    DetailVc.inter = indexPath.item;
    [self presentViewController:DetailVc animated:YES completion:^{
        
    }];
}
#pragma mark - cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArr.count;
}

#pragma mark - cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DestinationAlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"twoCell" forIndexPath:indexPath];
    
    [cell.postImgView sd_setImageWithURL:[NSURL URLWithString:[self.itemArr[indexPath.item] photo_1600]]];
    
    return cell;
    
}


@end
